<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>编辑校园风采</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea.form-control {
            height: 200px;
            resize: vertical;
            font-size: 14px;
            line-height: 1.5;
            font-family: Arial, sans-serif;
        }
        .btn-group {
            margin-top: 20px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .btn-primary {
            background-color: #1ab394;
            color: white;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-default {
            background-color: #f3f3f4;
            color: #333;
        }
        .btn-default:hover {
            background-color: #e7eaec;
        }
        .preview-image {
            max-width: 200px;
            max-height: 200px;
            margin-top: 10px;
            display: none;
        }
        .current-image {
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .checkbox-group {
            margin-top: 10px;
        }
        .checkbox-group label {
            display: inline-flex;
            align-items: center;
            margin-right: 20px;
            cursor: pointer;
        }
        .checkbox-group input[type="checkbox"] {
            margin-right: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2><i class="fas fa-edit"></i> 编辑校园风采</h2>
        <form id="styleForm" action="${pageContext.request.contextPath}/admin/campus/update" method="post" enctype="multipart/form-data">
            <input type="hidden" name="styleId" value="${style.styleId}">
            
            <div class="form-group">
                <label for="title">标题：</label>
                <input type="text" id="title" name="title" class="form-control" value="${style.title}" required>
            </div>
            
            <div class="form-group">
                <label for="image">图片：</label>
                <c:if test="${not empty style.imageUrl}">
                    <div class="current-image">
                        <img src="${pageContext.request.contextPath}/${style.imageUrl}" 
                             alt="${style.title}" style="max-width: 200px;">
                    </div>
                </c:if>
                <input type="file" id="image" name="image" class="form-control" accept="image/*" onchange="previewImage(this)">
                <img id="preview" class="preview-image">
            </div>
            
            <div class="form-group">
                <label for="description">描述：</label>
                <textarea id="description" name="description" class="form-control" required>${style.description}</textarea>
            </div>
            
            <div class="form-group">
                <label>状态设置：</label>
                <div class="checkbox-group">
                    <label>
                        <input type="checkbox" name="status" value="1" ${style.status == 1 ? 'checked' : ''}>
                        立即显示
                    </label>
                </div>
            </div>
            
            <div class="btn-group">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-default" onclick="history.back()">
                    <i class="fas fa-arrow-left"></i> 返回
                </button>
            </div>
        </form>
    </div>

    <script>
        function previewImage(input) {
            const preview = document.getElementById('preview');
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                }
                reader.readAsDataURL(input.files[0]);
            }
        }

        document.getElementById('styleForm').onsubmit = function(e) {
            e.preventDefault();
            
            const description = document.getElementById('description').value.trim();
            if (!description) {
                alert('请输入描述内容');
                return;
            }
            
            const formData = new FormData(this);
            // 设置默认值
            if (!formData.has('status')) {
                formData.append('status', '0');
            }
            
            fetch(this.action, {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('更新成功');
                    window.location.href = '${pageContext.request.contextPath}/admin/campus/list';
                } else {
                    alert(data.message || '更新失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('更新失败');
            });
        };
    </script>
</body>
</html> 